
clear
clear all
clear mata
clear matrix
set more off
set matsize 11000
set maxvar 30000
cap log off
capture log close
set emptycells drop
pause on


***********************************************
* USER DEFINE FILEPATH FOR REPLICATION FOLDER *
***********************************************

global path "C:\Users\wb520443\Dropbox\Research Projects\Global pollution\2_analysis\Replication"



***********************************************


global opts		a f plain coll(none) nodep nomti c(b(star fmt(%9.3f)) se(abs par fmt(%9.3f))  ) star(* .10 ** .05 *** .01) noobs nocons

global opts_fs		a f plain coll(none) nodep nomti c(b(star fmt(%9.3f)) se(abs par fmt(%9.3f))) star(* .10 ** .05 *** .01) noobs nocons


global input "$path\data"
global figdat "$path\figdat"
global figures "$path\figures"
global tables "$path\tables"
		
	use "$input//rwi_pollution_analysis_collapsed.dta", replace
	
	replace error=1/error
	g both=population*error
	g both2=country_population*error
	
	encode country, g(country_code)
	
	reghdfe mean_pm25 rwi, absorb(i.country_code)
		est store t1c1
	reghdfe mean_pm25 rwi [pweight=population], absorb(i.country_code)
		est store t1c2
	reghdfe mean_pm25 rwi [pweight=country_population], absorb(i.country_code)
		est store t1c3	
	reghdfe mean_pm25 rwi [pweight=error], absorb(i.country_code)
		est store t1c4
	reghdfe mean_pm25 rwi [pweight=both], absorb(i.country_code)
		est store t1c5
	reghdfe mean_pm25 rwi [pweight=both2], absorb(i.country_code)
		est store t1c6		
		
	reghdfe median_pm25 rwi, absorb(i.country_code)
		est store t2c1
	reghdfe median_pm25 rwi [pweight=population], absorb(i.country_code)
		est store t2c2
	reghdfe median_pm25 rwi [pweight=country_population], absorb(i.country_code)
		est store t2c3	
	reghdfe median_pm25 rwi [pweight=error], absorb(i.country_code)
		est store t2c4
	reghdfe median_pm25 rwi [pweight=both], absorb(i.country_code)
		est store t2c5
	reghdfe median_pm25 rwi [pweight=both2], absorb(i.country_code)
		est store t2c6	
		
	reghdfe max_pm25 rwi, absorb(i.country_code)
		est store t3c1
	reghdfe max_pm25 rwi [pweight=population], absorb(i.country_code)
		est store t3c2
	reghdfe max_pm25 rwi [pweight=country_population], absorb(i.country_code)
		est store t3c3	
	reghdfe max_pm25 rwi [pweight=error], absorb(i.country_code)
		est store t3c4
	reghdfe max_pm25 rwi [pweight=both], absorb(i.country_code)
		est store t3c5
	reghdfe max_pm25 rwi [pweight=both2], absorb(i.country_code)
		est store t3c6	
	
	file open  t	using "$tables/table_weighted.tex", replace write
		file write t	"\begin{table}[htbp] \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
					"\caption{Correlation between RWI and PM\$_{2.5}\$}\label{tab:table_weighted}" _n  ///
					"\begin{tabular*}{1.4\textwidth}{@{\extracolsep{\fill}}l*{8}{c}}" _n "\midrule" _n ///
"&Unweighted&\shortstack{Population\\weighted}&\shortstack{Country\\population\\weight}&\shortstack{Error\\weighted}&\shortstack{Jointly\\weighted}&\shortstack{Country\\jointly\\weighted}\\" _n ///
					"\midrule" _n 
		file close t
		
			
		file open  t 	using "$tables/table_weighted.tex", append write
		file write t 	"(A) PM\$_{2.5}\$ measured as average\\" _n "\cmidrule{1-1}" _n
		file close t
		
		esttab t1* using "$tables/table_weighted.tex", l keep(rwi) ///
			s(N, l("N") f(%11.0fc) lay(@)) $opts  
			
		file open  t 	using "$tables/table_weighted.tex", append write
		file write t 	"(B) PM\$_{2.5}\$ measured as median\\" _n "\cmidrule{1-1}" _n
		file close t
		
		esttab t2* using "$tables/table_weighted.tex", l keep(rwi) ///
			s(N, l("N") f(%11.0fc) lay(@)) $opts 	
			
		file open  t 	using "$tables/table_weighted.tex", append write
		file write t 	"(C) PM\$_{2.5}\$ measured as average during maximum month\\" _n "\cmidrule{1-1}" _n
		file close t
		
		esttab t3* using "$tables/table_weighted.tex", l keep(rwi) ///
			s(N, l("N") f(%11.0fc) lay(@)) $opts 	
		
		file open  t 	using "$tables/table_weighted.tex", append write
		file write t "\\" ///
						"\textbf{Fixed Effects:}&	&	&	&	&	&		\\" ///
						"County 				&Y	&Y	&Y	&Y 	&Y	&Y	 	\\" ///
						"\midrule" _n "\end{tabular*}" _n ///
						"\begin{tabular*}{1.4\textwidth}{p{8.8in}}" _n ///
						"\footnotesize \textsc{Notes:} Each column reports the results of a linear fixed effects regression of pollution against RWI at the gridcell level across all countries in our sample. Each row is a seperate regression. RWI is a country specific wealth index that ranges across our full dataset from -2.022 to 2.456. We collapse pollution across all months and years in the sample by RWI gridcell. In row \textbf{A} we collapse as the average across all months and years. In row \textbf{B} we collapse as the median. In row \textbf{C} we calculate the monthly average across all years and assign gridcells the average in the month with the highest monthly average. Population weights by the population at the RWI gridcells. Error weights by the inverse of the error reported by the creators of the RWI data for each RWI estimate. Joint weights by both population and the inverse of the error. Country population weights by the total population of the countries in which the gridcell is located. Country jointly weights by the country population and the inverse of the error. \textit{p}-values reported in brackets. (* p$<$.10 ** p$<$.05 *** p$<$.01)." ///
						"\end{tabular*}" _n "\end{table}" _n 
		file close t
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	